package net.uiiang.android.alkaid.helper;

import java.util.ArrayList;

import net.uiiang.android.alkaid.object.FileManagerDbObject;
import net.uiiang.android.alkaid.utils.FavoriteUtil;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class FileManagerDbAdapter {
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    private final Context mCtx;

    public static ArrayList<String> favoritePathList=null;
    
    public FileManagerDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    public FileManagerDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public long createFavorite(String filePath, String fileName, String alias,
            String fileType) {
        ContentValues initialValues = new ContentValues();
        Long now = Long.valueOf(System.currentTimeMillis());
        initialValues.put(FileManagerDbObject.Favorite.FILE_PATH, filePath);
        initialValues.put(FileManagerDbObject.Favorite.FILE_NAME, fileName);
        initialValues.put(FileManagerDbObject.Favorite.ALIAS, alias);
        initialValues.put(FileManagerDbObject.Favorite.FILE_TYPE, fileType);
        initialValues.put(FileManagerDbObject.Favorite.CREATED_DATE, now);

        return mDb.insert(FileManagerDbObject.Favorite.TABLE_NAME, null,
                initialValues);

    }

    public boolean deleteFavorite(long rowId) {
        return mDb.delete(FileManagerDbObject.Favorite.TABLE_NAME,
                FileManagerDbObject.Favorite._ID + "=" + rowId, null) > 0;
    }

    public Cursor fetchAllFavorite() {
//        convertFavoritePathTolist(c);
        Cursor c =mDb.query(FileManagerDbObject.Favorite.TABLE_NAME, new String[] {
                FileManagerDbObject.Favorite._ID,
                FileManagerDbObject.Favorite.FILE_PATH,
                FileManagerDbObject.Favorite.FILE_NAME,
                FileManagerDbObject.Favorite.FILE_TYPE,
                FileManagerDbObject.Favorite.ALIAS }, null, null, null, null,
                null);
        favoritePathList = FavoriteUtil.convertCursorToPathList(c);
        
        return c;
    }

    public Cursor fetchFavorite(long rowId) throws SQLException {

        Cursor mCursor =

        mDb.query(true, FileManagerDbObject.Favorite.TABLE_NAME, new String[] {
                FileManagerDbObject.Favorite._ID,
                FileManagerDbObject.Favorite.FILE_PATH,
                FileManagerDbObject.Favorite.FILE_NAME,
                FileManagerDbObject.Favorite.FILE_TYPE,
                FileManagerDbObject.Favorite.ALIAS },
                FileManagerDbObject.Favorite._ID + "=" + rowId, null, null,
                null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    public boolean updateFavorite(long rowId, String alias) {
        ContentValues args = new ContentValues();
        args.put(FileManagerDbObject.Favorite.ALIAS, alias);

        return mDb.update(FileManagerDbObject.Favorite.TABLE_NAME, args,
                FileManagerDbObject.Favorite._ID + "=" + rowId, null) > 0;
    }

    public void close() {
        mDb.close();
        mDbHelper.close();
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, FileManagerDbObject.DB_NAME, null,
                    FileManagerDbObject.DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(FileManagerDbObject.Favorite.CREATE_TABLE_STR);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS "
                    + FileManagerDbObject.Favorite.TABLE_NAME);
            onCreate(db);
        }
    }
}
